/**
 * 订阅测试
 */
import { create } from "zustand";
import { subscribeWithSelector } from "zustand/middleware";
interface Store {
  count: number;
  age: number;
  increment: () => void;
  incrementAge: () => void;
  decrement: () => void;
  incrementBy: (amount: number) => void;
}

export const useStore = create<Store>((set) => ({
  count: 0,
  age: 0,
  increment: () => set((state) => ({ count: state.count + 1 })),
  incrementAge: () => set((state) => ({ count: state.age + 1 })),
  decrement: () => set((state) => ({ count: state.count - 1 })),
  incrementBy: (amount: number) =>
    set((state) => ({ count: state.count + amount })),
}));

// 中间件可以只订阅单个值
export const useStore2 = create(
  subscribeWithSelector<Store>((set) => ({
    count: 0,
    age: 0,
    increment: () => set((state) => ({ count: state.count + 1 })),
    incrementAge: () => set((state) => ({ count: state.age + 1 })),
    decrement: () => set((state) => ({ count: state.count - 1 })),
    incrementBy: (amount: number) =>
      set((state) => ({ count: state.count + amount })),
  }))
);
